﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace RgUtility.Orm.FastSetter
{
    /// <summary>
    /// Setter的包装器
    /// </summary>
    /// <typeparam name="TTarget"></typeparam>
    /// <typeparam name="TValue"></typeparam>
    public  class SetterWrapper<TTarget, TValue> : ISetValue
    {
        private Action<TTarget, TValue> _setter;

        public SetterWrapper(PropertyInfo propertyInfo)
        {
            MethodInfo m = propertyInfo.GetSetMethod(true);//得到方法
            _setter = (Action<TTarget, TValue>)
                Delegate.CreateDelegate(typeof(Action<TTarget, TValue>),//创建的委托类型
                null,//委托所属类。null表示是静态委托。静态的访问更加快捷。
                m//委托代表的方法。
                );
        }

        public void SetValue(TTarget target, TValue val)
        {
            _setter(target, val);
        }

        public void Set(object target, object val)
        {
            _setter((TTarget)target, (TValue)val);
        }
    }
}
